package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dto.DishDto;
import com.itheima.po.Category;
import com.itheima.po.Dish;
import com.itheima.po.DishFlavor;
import com.itheima.po.R;
import com.itheima.service.CategoryService;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;
    @PostMapping("")
    public R<String> save(@RequestBody DishDto dishDto){
        dishService.saveWithFlavor(dishDto);
        return R.success("添加成功");
    }
//    分页查询
    @GetMapping("/page")
    public R<Page<DishDto>> page(Integer page, Integer pageSize,String name){
//        根据查询条件查询出菜品数据
        Page<Dish> dishPage = new Page<>(page, pageSize);
//        设置查询条件--模糊查询
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        isNotBlank过滤掉"" , " " , null , "     "
        lambdaQueryWrapper.like(StringUtils.isNotBlank(name),Dish::getName,name);
        dishService.page(dishPage,lambdaQueryWrapper);
//        根据菜品数据中的分类ID查询分类数据
//        把Dish的值复制给DishDto
//        因为dishPage没有categoryName,所以需要把dishPage的值复制给dishDtoPage
        Page<DishDto> dishDtoPage = new Page<>();//真正要返回给前端的
       /* dishDtoPage.setRecords(new ArrayList<>());
//        从数据库中差回来的分页的dish数据
        List<Dish> records = dishPage.getRecords();
        for (int i = 0; i < records.size(); i++) {
            DishDto dishDto = new DishDto();
            dishDto.setCategoryId(records.get(i).getCategoryId());
            dishDto.setImage(records.get(i).getImage());
            dishDto.setCategoryId(records.get(i).getCategoryId());
            dishDto.setCategoryId(records.get(i).getCategoryId());
            dishDto.setCategoryId(records.get(i).getCategoryId());
            dishDto.setCategoryId(records.get(i).getCategoryId());
            dishDtoPage.getRecords().add(dishDto);
        }
        dishDtoPage.setTotal(dishPage.getTotal());
        dishDtoPage.setCurrent(dishPage.getCurrent());
        dishDtoPage.setSize(dishPage.getSize());*/
//        忽略records，那么复制完后records没有复制过来，所以后面需要单独处理records的值
        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");
        dishDtoPage.setRecords(new ArrayList<>());
//        给每个菜品的DTO设置分类名字
        List<Dish> records = dishPage.getRecords();
        for (Dish dish : records) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish,dishDto);
            Category category = categoryService.getById(dishDto.getCategoryId());
            if(null != category){
                dishDto.setCategoryName(category.getName());
            }
            dishDtoPage.getRecords().add(dishDto);
        }
        return R.success(dishDtoPage);
    }
//    删除
    @DeleteMapping("")
    public R<String> delete(Long id){

        return R.success("删除成功");
    }


//    修改
    @PutMapping
    public R<String> update(@RequestBody Category category){
        return R.error("修改成功");
    }
//    查询
    @GetMapping("/{id}")
    public R<DishDto> getById(@PathVariable Long id){
//        查询菜品数据
        Dish dish = dishService.getById(id);
//        根据菜品ID查询口味数据
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> dishFlavorList = dishFlavorService.list(lambdaQueryWrapper);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(dishFlavorList);
        return R.success(dishDto);
    }
//更新
    @PutMapping("")
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        return R.success("更新成功");
    }
    @GetMapping("/list")
    public R<List<Dish>> list(Long categoryId ){
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(null != categoryId, Dish::getCategoryId,categoryId);
        List<Dish> list = dishService.list(lambdaQueryWrapper);
        return R.success(list);
    }
}
